﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using SlimDX;
using SlimDX.Direct3D9;
using GameStudio.Framework.GraphicsEngine;
using System.IO;

namespace GameStudio
{
    public class FillMesh
    {
        public static List<Mesh<Vertex>> _mesh;
        private List<Vertex> vertices;
        private Mesh<Vertex> NewMesh;
        static private List<Texture> textures;
        public static List<string> MyColors;

        public void ResetMeshes()
        {
            _mesh = new List<Mesh<Vertex>>();
        }
        public void ReadXmlImage(string Path)
        {
            NewMesh = new Mesh<Vertex>();
            vertices = new List<Vertex>();
            textures = new List<Texture>();
            MyColors = new List<string>();

            string Positions;
            ColorConverter Conv = new ColorConverter();
            string ColorShape;
            XmlDocument doc = new XmlDocument();
            doc.Load(Path);
            Vertex vertex;
            foreach (System.Xml.XmlElement node in doc.GetElementsByTagName("Voxel"))
            {
                vertex = new Vertex();
                Positions = node.Attributes.GetNamedItem("Position").Value;
                ColorShape = node.Attributes.GetNamedItem("Colour").Value;

                MyColors.Add(ColorShape);
                //vertex.Color = (Color3)Conv.ConvertFromString(ColorShape);//exception..how to convert string to color3
                vertex.Position = AddVertex(Positions);
                //vertex.Position=new Vector3(0.2f ,0.3f ,0.5f);
                vertices.Add(vertex);
            }
        }

        public static void AddTextures(Bitmap bmp)//call this method when user load bmp 
        {
            textures.Add(AddBmpTexture(bmp));
        }

        public void AssignObjectToUser()
        {
            
            NewMesh.VertexBuffer = vertices.ToArray();
            //how to assign arr of textures to NewMesh.Textures??! --> Textures be property as VertexBuffer
            //and Materials ??

            _mesh.Add(NewMesh);
        }

        private Vector3 AddVertex(string Nodes)
        {
            string[] Positions;

            Positions = Nodes.Split(',');

            Vector3 MyVector = new Vector3((float)Convert.ToDouble(Positions[0]),
                (float)Convert.ToDouble(Positions[1]),
                (float)Convert.ToDouble(Positions[2]));

            return MyVector;
        }


        public static Texture AddBmpTexture(Bitmap picture)
        {
            var byteBMP = new MemoryStream(imageToByteArray(picture));
            Texture text = SlimDX.Direct3D9.Texture.FromStream(DeviceContext.Current.Device, byteBMP);
            return text;
        }


        public static byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
    }
}

